<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item> 商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 输入框和添加按钮 -->
      <el-row :gutter="20">
        <!-- 搜索按钮 -->
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="queryinfo.query"
            clearable
            @clear="getGoodslist"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodslist"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoodsEvent">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodslist" stripe style="width: 100%" border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          :width="600"
        ></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
        ></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="150px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" :width="130">
          <template>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";
// import AddGoods from "../Goods/AddGoods.vue";
export default {
  data() {
    return {
      //所有的商品列表
      goodslist: [],
      //获取商品列表传递的参数
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      // pagenum:1,
      //设置添加商品对话框默认值
      // addGoodsDialogVisible: false,
      // 搜索输入框的值
      inputvalue: "",
    };
  },
  created() {
    this.getGoodslist();
  },
  methods: {
    // 获取商品列表
    getGoodslist() {
      axios
        .get("goods", { params: this.queryinfo })
        .then(({ data: res }) => {
          console.log(res);
          this.goodslist = res.data.goods;
          this.total = res.data.total;
        })
        .catch((error) => error);
    },
    // 更改每页条数的事件
    handleSizeChange(newsize) {
      this.queryinfo.pagesize = newsize;
      this.getGoodslist();
      console.log(this.queryinfo.pagesize);
    },
    // 更改页数的条数事件
    handleCurrentChange(newnum) {
      this.queryinfo.pagenum = newnum;
      this.getGoodslist();
      console.log(this.queryinfo.pagenum);
    },

    // 添加商品实现路由跳转
    addGoodsEvent() {
      this.$router.push("/goods/add");
    },
  },
};
</script>

<style lang="scss" scoped>
.el-table {
  font-size: 12px;
}
</style>